package com.test.exam2;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class ElementJoinerImpl implements ElementJoiner {

	public <T> String join(T[] ts, String delimiter) {
		StringBuilder sbd = new StringBuilder();
		for (int i = 0; i < ts.length; i++) {
			sbd.append(String.valueOf(ts[i])).append(delimiter);
		}
		return sbd.substring(0, sbd.length() - delimiter.length());
	}

	public <T> String join(Collection<T> ts, String delimiter) {
		StringBuilder sbd = new StringBuilder();
		Iterator<T> iter = ts.iterator();
		while (iter.hasNext()) {
			sbd.append(iter.next()).append(delimiter);
		}
		return sbd.substring(0, sbd.length() - delimiter.length());
	}

	public static void main(String[] args) {
		Integer[] a = new Integer[10];
		for (int i = 0; i < a.length; i++) {
			a[i] = i;
		}

		ElementJoinerImpl impl = new ElementJoinerImpl();
		System.out.println(impl.join(a, "[HH]"));
		System.out.println(impl.join(Arrays.asList(a), "|"));
	}
}

// public class ElementJoinerImpl implements ElementJoiner {
//
//
// public ElementJoinerImpl() {
// super();
// }
//
// public <T> String join(T[] ts, String delimiter) {
// StringBuffer sb = new StringBuffer();
// for (int i = 0; i < ts.length; i++) {
// sb.append(String.valueOf(ts[i])).append(delimiter);
// }
// return sb.substring(0, sb.length()-delimiter.length());
// }
//
// public <T> String join(Collection<T> ts, String delimiter) {
// StringBuilder sbd = new StringBuilder();
// Iterator<T> iter = ts.iterator();
// while(iter.hasNext()) {
// sbd.append(iter.next()).append(delimiter);
// }
// return sbd.substring(0, sbd.length()-delimiter.length());
// }
//
// }
